package com.frame.hander;

import java.util.ArrayList;
import java.util.List;

import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;

/**
 * @author LRC
 * @createDate 2015年5月24日
 * @description 系统信息获取类
 */
public class SystemInfoHander {

	public static Computer getComputerInfo() {
		Computer c = new Computer();
		Sigar sigar = new Sigar();
		Mem mem = null;
		Swap swap = null;
		FileSystem fslist[] = null;
		try {
			mem = sigar.getMem();
			swap = sigar.getSwap();
			fslist = sigar.getFileSystemList();
		} catch (SigarException e) {
			e.printStackTrace();
			return null;
		}
		c.memMax = mem.getTotal() / (1024 * 1024L);
		c.memUsed = mem.getUsed() / (1024 * 1024L);
		c.swapMax = swap.getTotal() / (1024 * 1024L);
		c.swapUsed = swap.getUsed() / (1024 * 1024L);
		// 获取分区信息
		List<Partition> partitions = new ArrayList<Partition>();
		for (FileSystem fs : fslist) {
			FileSystemUsage usage = null;
			try {
				usage = sigar.getFileSystemUsage(fs.getDirName());
			} catch (SigarException e) {
				//e.printStackTrace();
				continue;
			}
			Partition p = new Partition();
			switch (fs.getType()) {
			case 0: // TYPE_UNKNOWN ：未知
				break;
			case 1: // TYPE_NONE
				break;
			case 2: // TYPE_LOCAL_DISK : 本地硬盘
				p.name = fs.getDirName();
				p.max = usage.getTotal()/1024L;
				p.used = usage.getUsed()/1024L;
				break;
			case 3:// TYPE_NETWORK ：网络
				break;
			case 4:// TYPE_RAM_DISK ：闪存
				break;
			case 5:// TYPE_CDROM ：光驱
				break;
			case 6:// TYPE_SWAP ：页面交换
				break;
			}
			partitions.add(p);
		}
		c.partitions = partitions;
		//jvm
		Runtime r = Runtime.getRuntime();
		c.jvmMax = r.totalMemory()/(1024*1024L);
		c.jvmUsed = (r.totalMemory() - r.freeMemory())/(1024*1024L);
		
		return c;
	}
	
	
	/*******************系统信息类定义**************************/
	public static class Computer {
		private double memMax;// 系统内存 单位M
		private double memUsed;// 当前使用内存 单位M
		private double swapMax;// 当前使用内存 单位M
		private double swapUsed;// 当前使用内存 单位M
		private double jvmMax;
		private double jvmUsed;
		public double getJvmMax() {
			return jvmMax;
		}
		public void setJvmMax(double jvmMax) {
			this.jvmMax = jvmMax;
		}
		public double getJvmUsed() {
			return jvmUsed;
		}
		public void setJvmUsed(double jvmUsed) {
			this.jvmUsed = jvmUsed;
		}
		public List<Partition> partitions;
		public double getMemMax() {
			return memMax;
		}
		public void setMemMax(double memMax) {
			this.memMax = memMax;
		}
		public double getMemUsed() {
			return memUsed;
		}
		public void setMemUsed(double memUsed) {
			this.memUsed = memUsed;
		}
		public double getSwapMax() {
			return swapMax;
		}
		public void setSwapMax(double swapMax) {
			this.swapMax = swapMax;
		}
		public double getSwapUsed() {
			return swapUsed;
		}
		public void setSwapUsed(double swapUsed) {
			this.swapUsed = swapUsed;
		}
		public List<Partition> getPartitions() {
			return partitions;
		}
		public void setPartitions(List<Partition> partitions) {
			this.partitions = partitions;
		}
	}

	public static class Partition {
		private String name;
		private double max;
		private double used;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public double getMax() {
			return max;
		}
		public void setMax(double max) {
			this.max = max;
		}
		public double getUsed() {
			return used;
		}
		public void setUsed(double used) {
			this.used = used;
		}
	}
}
